/*
 * misc.c
 *
 *  Created on: 03/05/2013
 *      Author: jose
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ipq_api.h>
#include <dwr_misc.h>

DwrString *
dwr_string_create_no_len(const char * str)
{
	assert(str);

	DwrString * s;

	s = (DwrString *) calloc(1, sizeof(DwrString));

	s->len = strlen(str);

	assert(s->len > 0);

	s->text = (char *) calloc((s->len), sizeof(char));
	memcpy(s->text, str, sizeof(char) * s->len );

	return s;
}


DwrString *
dwr_string_create_with_len(const char * str, const unsigned len)
{
	assert(str);
	assert(len > 0);

	DwrString * s = NULL;

	s = (DwrString *) calloc(1, sizeof(DwrString));

	s->len = len;

	s->text = (char *) calloc((s->len), sizeof(char));
	memcpy(s->text, str, sizeof(char) * s->len );

	return s;
}

DwrString *
dwr_string_itoa(const int value, const char * format, const unsigned len)
{
	assert(format);
	assert(len > 0);

	DwrString * s = NULL;
	u8 * string = NULL;

	string = (u8 *) calloc(len+1, sizeof(u8));

	if (!string)
		return NULL;

	sprintf(string, format, value);

	s = dwr_string_create_with_len(string, len);

	return s;
}

unsigned char *
dwr_char_itoa(const int value, const char * format, const unsigned len)
{
	assert(format);
	assert(len > 0);

	unsigned char * string = (unsigned char *) calloc(len, sizeof(unsigned char));

	if (!string)
		return NULL;

	sprintf(string, format, value);

	return string;
}

DwrString *
dwr_string_concat(DwrString * s1, const DwrString * s2)
{
	assert(s1);
	assert(s2);

	s1->text = (char *) realloc(s1->text, (s1->len + s2->len) * sizeof(char));

	if (!(s1->text))
		return NULL;

	memcpy(&(s1->text[s1->len]), s2->text, s2->len);
	s1->len += s2->len;

	return s1;
}

DwrString *
dwr_string_concat_comma(DwrString * s1, short unsigned * previous,
		const char * s2)
{
	assert(s1);
	assert(s2);
	assert( (*previous) >= 0);
	assert( (*previous) < 2);

	DwrString * comma_string = dwr_string_create_with_len(", ", 2);
	DwrString * s2_string = dwr_string_create_no_len(s2);

	if (!comma_string || !s2_string)
		return NULL;

	if (*previous)
	{
		s1 = dwr_string_concat(s1, comma_string);
		if (!s1)
			return NULL;
	}
	else
		(*previous) = 1;

	s1 = dwr_string_concat(s1, s2_string);

	if (!s1)
		return NULL;

	return s1;
}

DwrString *
dwr_string_concat_comma_quote(DwrString * s1, short unsigned * previous,
		const char * s2)
{
	assert(s1);
	assert(s2);
	assert( (*previous) >= 0);
	assert( (*previous) < 2);

	DwrString * comma_string = dwr_string_create_with_len(", ", 2);
	DwrString * quote_string = dwr_string_create_with_len("\'", 1);
	DwrString * s2_string = dwr_string_create_no_len(s2);

	if (!comma_string || !quote_string || !s2_string)
		return NULL;

	if (*previous)
	{
		s1 = dwr_string_concat(s1, comma_string);
		if (!s1)
			return NULL;
	}
	else
		(*previous) = 1;

	s1 = dwr_string_concat(s1, quote_string);
	s1 = dwr_string_concat(s1, s2_string);
	s1 = dwr_string_concat(s1, quote_string);

	if (!s1)
		return NULL;

	return s1;
}

unsigned char *
dwr_string_get_text(DwrString * str)
{
	assert(str);

	unsigned char * s = (unsigned char *) calloc(str->len+1, sizeof(unsigned char));

	if (!s)
		return NULL;

	memcpy(s, str->text, str->len+1);

	return s;
}

#ifdef DWR_DEBUG
void dwr_log_char(const char *file, const char *func,  const int line,
		const char *description, const char *aux){
	printf("\n--DWR LOG:%s:%s:%d: %s %s\n", file, func, line, description, aux);
}

void dwr_log_string(const char *file, const char *func,  const int line,
		const char *description, const DwrString *aux){
	printf("\n--DWR LOG:%s:%s:%d: %s %.*s\n", file, func, line, description, aux->len, aux->text);
}
#endif
